package com.weixin.xplore.dao.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.weixin.xplore.dao.entity.SignInRecord;
import com.weixin.xplore.dao.entity.SysUser;
import com.weixin.xplore.dao.entity.UserInvitation;
import com.weixin.xplore.dao.service.*;
import com.weixin.xplore.helper.IntegralHelper;
import com.weixin.xplore.service.SysSettingBizService;
import com.weixin.xplore.utils.JsonUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;

@Service
@RequiredArgsConstructor
public class PointOperatorServiceImpl implements PointOperatorService {
    private final SysUserDaoService sysUserDaoService;
    private final IntegralHelper integralHelper;
    private final IntegralRecordDaoService recordDaoService;
    private final SysSettingBizService settingBizService;

    private final UserInvitationDaoService userInvitationDaoService;
    private final SignInRecordDaoService signInRecordDaoService;

    @Override
    public Boolean invitation(String invitationCode, String userId) {
        SysUser sysUser1 = sysUserDaoService.queryByInvitation(invitationCode);
        if (ObjectUtil.isNotNull(sysUser1)) {
            String extend = sysUser1.getExtend();
            Map<String, Object> map = JsonUtil.toMap(extend, String.class, Object.class);
            //增加邀请积分
            Long currentPoints = MapUtil.getLong(map, "currentPoints");
            if (ObjectUtil.isNull(currentPoints)) {
                currentPoints = 0L;
            }
            BigDecimal realIntegral = integralHelper.getRealIntegral(1L);
            map.put("currentPoints", currentPoints + realIntegral.longValue());
            sysUserDaoService.updatePopint(sysUser1.getId(), map);
            recordDaoService.saveLog(sysUser1.getId(), 9, realIntegral.longValue());
            //增加邀请记录
            UserInvitation userInvitation = new UserInvitation();
            userInvitation.setUserId(userId);
            userInvitation.setInviterId(sysUser1.getId());
            userInvitation.setInviteeId(userId);
            BigDecimal bigDecimal = settingBizService.queryValueByKey("user.Inviter.rebateRatio", BigDecimal.class, new BigDecimal(0.1));
            userInvitation.setRebateRatio(bigDecimal);
            userInvitationDaoService.save(userInvitation);
            //被邀请人也需要积分
        }
        return Boolean.TRUE;
    }

    @Override
    public Boolean sign(String id) {
        SysUser sysUser = sysUserDaoService.queryById(id);

        //获取签到记录是不是连续签到7天
        Map<String, Object> map = JsonUtil.toMap(sysUser.getExtend(), String.class, Object.class);
        Date lastSignDate = MapUtil.getDate(map, "lastSignDate");
            if(ObjectUtil.isNotNull(lastSignDate)){
                Assert.isFalse(DateUtil.isSameDay(lastSignDate, new Date()), "同一天不能重复签到");
            }

        Long contineSign = settingBizService.queryValueByKey("user.continueSign", Long.class, 7L);
        Boolean continueSign = sysUser.isContinueSign(contineSign);
        Boolean isVip = sysUser.getIsVip();

        Long jf = 15L;
        if (BooleanUtil.isTrue(continueSign)) {
            jf += 30;

        }
        map.put("lastSignDate", DateUtil.date());
        map.put("continueSignDay", MapUtil.getLong(map, "continueSignDay", 0L) + 1L);
        if (BooleanUtil.isTrue(isVip)) {
            jf += 5;
            if (BooleanUtil.isTrue(continueSign)) {
                jf += 10;
            }
        }
        Long currentPoints = MapUtil.getLong(map, "currentPoints");

        map.put("currentPoints", currentPoints + jf);
        sysUserDaoService.updatePopint(id, map);
        recordDaoService.saveLog(id, 1, jf, "签到");
        //增加签到记录
        SignInRecord sign = new SignInRecord();
        sign.setUserId(sysUser.getId());
        sign.setSignInTime(DateUtil.date());
        sign.setIsContinuous(continueSign ? 1 : 0);
        signInRecordDaoService.save(sign);
        return Boolean.TRUE;
    }
}
